package com.xiaosu.red_travel.bean;

import org.junit.Test;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

public class Test02 {

    private String filepath = "C:\\Users\\小苏\\Desktop\\test2";

    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(3000);

        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection("哈哈哈"), null);

        try {
            Robot robot = new Robot();
            for (int i = 0; i < 10; i++) {
                robot.keyPress(KeyEvent.VK_CONTROL);
                robot.keyPress(KeyEvent.VK_V);
                robot.keyRelease(KeyEvent.VK_V);

                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyRelease(KeyEvent.VK_ENTER);

                robot.keyRelease(KeyEvent.VK_CONTROL);
                robot.keyRelease(KeyEvent.VK_V);
                Thread.sleep(1);
                robot.keyRelease(KeyEvent.VK_CONTROL);
                robot.keyRelease(KeyEvent.VK_V);
            }

        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    public void jdbc(){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); //加载驱动
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
            Statement statement = con.createStatement();//创建Statement对象
            ResultSet rs = statement.executeQuery("select * from test"); //执行sql查询语句，返回结果集
            while(rs.next()) { //循环操作结果集
                String string = rs.getString("id") + "——" + rs.getString("name");
                System.out.println(string);
            }
            rs.close();//关闭结果集
            statement.close();//关闭Statement对象
            con.close();//关闭与数据库的连接
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void fileTest() throws IOException {
        File file = new File("C:\\Users\\小苏\\Desktop\\test");

        boolean directory = file.isDirectory();
        if(!directory){
            System.out.println("该文件不是文件夹");
        }
        TreeMap<String, File> map = new TreeMap<>();
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            Date d = new Date(files[i].lastModified());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
            String format = simpleDateFormat.format(d);
            File put = map.put(format, files[i]);
        }
        //long l = file.lastModified();
        File file2 = new File(filepath);
        if(!file2.exists()){
            System.out.println("jjjjj");
            file2.mkdir();
        }
        int i = 0;
        for (String s : map.keySet()) {
            i++;
            File file1 = map.get(s);
            String name = file1.getName();
            String newFileName = "";

            try {
                String substring = name.substring(0, 2);
                int i1 = Integer.parseInt(substring);
                newFileName = i+"-"+name.substring(2);
            }catch (Exception e){
                try {
                    String substring = name.substring(0, 1);
                    int i1 = Integer.parseInt(substring);
                    newFileName = i + "-" + name.substring(1);
                }catch (Exception e1){
                    newFileName = i + "-" + name;
                }
            }

            File newfile = new File("C:\\Users\\小苏\\Desktop\\test\\"+newFileName);
            boolean b = file1.renameTo(newfile);
            if(b){
                System.out.println("成功");
            }else {
                System.out.println("失败");
            }
            /*String substring = name.substring(0,2);
            FileInputStream fileInputStream = new FileInputStream(map.get(s));
            File file3 = new File(filepath + "\\" + i + "-" + name);
            if(file3.exists()){
                file3.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file3);

            byte[] arr = new byte[1024];
            while(fileInputStream.read(arr)!=-1){
                fileOutputStream.write(arr);
            }

            fileInputStream.close();
            fileOutputStream.close();*/

        }

    }

    public void test02(){
        List<Integer> arr = new ArrayList<>();
    }

}
